package dao;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * A data access object (DAO) providing persistence and search support for
 * UserTab entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see dao.UserTab
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class UserTabDAO {
	private static final Logger log = LoggerFactory.getLogger(UserTabDAO.class);
	// property constants
	public static final String USER_ACC = "userAcc";
	public static final String PWD_NOR = "pwdNor";
	public static final String PWD_PHO = "pwdPho";
	public static final String TRUE_NAME = "trueName";
	public static final String ID_NUMBER = "idNumber";
	public static final String PHONE = "phone";
	public static final String MAIL = "mail";
	public static final String QUESTION1 = "question1";
	public static final String ANSWER1 = "answer1";
	public static final String QUESTION2 = "question2";
	public static final String ANSWER2 = "answer2";
	public static final String QUESTION3 = "question3";
	public static final String ANSWER3 = "answer3";
	public static final String SECURITY_LV = "securityLv";
	public static final String ACCOUNT_STATUS = "accountStatus";
	public static final String EMAIL_URL = "emailUrl";
	public static final String SECRET_EMAIL_STATUS = "secretEmailStatus";
	public static final String SECRET_EMAIL_URL = "secretEmailUrl";
	public static final String PWD_BACK_CODE = "pwdBackCode";
	public static final String AVATAR = "avatar";
	public static final String ADMIN_LVL = "adminLvl";
	public static final String NICK_NAME = "nickName";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(UserTab transientInstance) {
		log.debug("saving UserTab instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(UserTab persistentInstance) {
		log.debug("deleting UserTab instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public UserTab findById(java.lang.Integer id) {
		log.debug("getting UserTab instance with id: " + id);
		try {
			UserTab instance = (UserTab) getCurrentSession().get("dao.UserTab",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<UserTab> findByExample(UserTab instance) {
		log.debug("finding UserTab instance by example");
		try {
			List<UserTab> results = (List<UserTab>) getCurrentSession()
					.createCriteria("dao.UserTab").add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UserTab instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from UserTab as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<UserTab> findByUserAcc(Object userAcc) {
		return findByProperty(USER_ACC, userAcc);
	}

	public List<UserTab> findByPwdNor(Object pwdNor) {
		return findByProperty(PWD_NOR, pwdNor);
	}

	public List<UserTab> findByPwdPho(Object pwdPho) {
		return findByProperty(PWD_PHO, pwdPho);
	}

	public List<UserTab> findByTrueName(Object trueName) {
		return findByProperty(TRUE_NAME, trueName);
	}

	public List<UserTab> findByIdNumber(Object idNumber) {
		return findByProperty(ID_NUMBER, idNumber);
	}

	public List<UserTab> findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List<UserTab> findByMail(Object mail) {
		return findByProperty(MAIL, mail);
	}

	public List<UserTab> findByQuestion1(Object question1) {
		return findByProperty(QUESTION1, question1);
	}

	public List<UserTab> findByAnswer1(Object answer1) {
		return findByProperty(ANSWER1, answer1);
	}

	public List<UserTab> findByQuestion2(Object question2) {
		return findByProperty(QUESTION2, question2);
	}

	public List<UserTab> findByAnswer2(Object answer2) {
		return findByProperty(ANSWER2, answer2);
	}

	public List<UserTab> findByQuestion3(Object question3) {
		return findByProperty(QUESTION3, question3);
	}

	public List<UserTab> findByAnswer3(Object answer3) {
		return findByProperty(ANSWER3, answer3);
	}

	public List<UserTab> findBySecurityLv(Object securityLv) {
		return findByProperty(SECURITY_LV, securityLv);
	}

	public List<UserTab> findByAccountStatus(Object accountStatus) {
		return findByProperty(ACCOUNT_STATUS, accountStatus);
	}

	public List<UserTab> findByEmailUrl(Object emailUrl) {
		return findByProperty(EMAIL_URL, emailUrl);
	}

	public List<UserTab> findBySecretEmailStatus(Object secretEmailStatus) {
		return findByProperty(SECRET_EMAIL_STATUS, secretEmailStatus);
	}

	public List<UserTab> findBySecretEmailUrl(Object secretEmailUrl) {
		return findByProperty(SECRET_EMAIL_URL, secretEmailUrl);
	}

	public List<UserTab> findByPwdBackCode(Object pwdBackCode) {
		return findByProperty(PWD_BACK_CODE, pwdBackCode);
	}

	public List<UserTab> findByAvatar(Object avatar) {
		return findByProperty(AVATAR, avatar);
	}

	public List<UserTab> findByAdminLvl(Object adminLvl) {
		return findByProperty(ADMIN_LVL, adminLvl);
	}

	public List<UserTab> findByNickName(Object nickName) {
		return findByProperty(NICK_NAME, nickName);
	}

	public List findAll() {
		log.debug("finding all UserTab instances");
		try {
			String queryString = "from UserTab";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public UserTab merge(UserTab detachedInstance) {
		log.debug("merging UserTab instance");
		try {
			UserTab result = (UserTab) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(UserTab instance) {
		log.debug("attaching dirty UserTab instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(UserTab instance) {
		log.debug("attaching clean UserTab instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static UserTabDAO getFromApplicationContext(ApplicationContext ctx) {
		return (UserTabDAO) ctx.getBean("UserTabDAO");
	}
}